

//
//  HMComposePhotosView.m
//  黑马微博-wzy
//
//  Created by Mac on 2017/2/13.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "HMComposePhotosView.h"

@implementation HMComposePhotosView

- (void)addImage:(UIImage *)image{
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    imageView.image = image;
    [self addSubview:imageView];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    int count = (int)self.subviews.count;
    //一行的最大列数
    int maxClosPerRow = 4;
    //每个图片之间的间距
    CGFloat margin = 10;
    //每个图片的宽高
    CGFloat imageViewW = (kScreenWidth - (maxClosPerRow + 1) * margin) / maxClosPerRow;
    CGFloat imageViewH = imageViewW;
    
    for (int i = 0; i < count; i++) {
        //行号
        int row = i / maxClosPerRow;
        //列号
        int col = i % maxClosPerRow;
        UIImageView *imageView = self.subviews[i];
        imageView.width = imageViewW;
        imageView.height = imageViewH;
        imageView.y = row * (imageViewH + margin);
        imageView.x = col * (imageViewW + margin) + margin;
    }

}

- (NSArray *)images{
    NSMutableArray *array = [NSMutableArray array];
    for (UIImageView *imageView in self.subviews) {
        [array addObject:imageView.image];
    }
    return array;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
